package com.tanhua.admin.module.dynamicmanage.controller;


import cn.hutool.core.collection.CollUtil;
import com.tanhua.admin.module.login.vo.ErrorResult;
import com.tanhua.admin.module.dynamicmanage.service.impl.TopMessageServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/manage")
public class TopController {
    @Autowired
    private TopMessageServiceImpl topMessageService;

    /**
     * 消息置顶
     * @param id
     * @return
     */
    @PostMapping("/messages/{id}/top")
    public ResponseEntity<Object> topMessage(@PathVariable("id") String id) {

        Map<String, String> resultMap = topMessageService.topState(id);
        if (!CollUtil.isEmpty(resultMap)) {
            //修改置顶状态成功
            return ResponseEntity.ok(resultMap);
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000002").errMessage("修改失败!").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }


    /**
     * 消息取消置顶
     * @param id
     * @return
     */
    @PostMapping("/messages/{id}/untop")
    public ResponseEntity<Object> upTopMessage(@PathVariable("id") String id) {

        Map<String, String> resultMap = topMessageService.unTopState(id);
        if (!CollUtil.isEmpty(resultMap)) {
            //修改置顶状态成功
            return ResponseEntity.ok(resultMap);
        }
        ErrorResult errorResult = ErrorResult.builder().errCode("000002").errMessage("修改失败!").build();
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }
}
